﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace KaixinMobile.Forms
{
    using Kaixin;
    using Kaixin.Tools;
    using Kaixin.House.Garden;

    public partial class frmMain : Form
    {
        private bool HadLogin = false;
        public KaiXin001 ka = new KaiXin001();

        private frmGarden fGarden = null;
        private frmRanch fRanch = null;

        public frmMain()
        {
            InitializeComponent();

            //pbGarden.Image = Properties.Resources.garden;
            //pbRanch.Image = Properties.Resources.ranch;

            this.Icon = Properties.Resources.kaixin;

            ilImage.Images.Add(Properties.Resources.garden);
            ilImage.Images.Add(Properties.Resources.ranch);

            ListViewItem li = lvMain.Items.Add(new ListViewItem("花园"));
            li.ImageIndex = 0;

            li = lvMain.Items.Add(new ListViewItem("农场"));
            li.ImageIndex = 1;

            TextOutput.SetListBox(lbResult);
        }
        
        private void menuItem5_Click(object sender, EventArgs e)
        {
            if (!HadLogin)
            {
                frmLogin frm = new frmLogin(this);
                frm.ShowDialog();

                Cursor.Current = Cursors.WaitCursor;
                if (frm.DialogResult == DialogResult.OK)
                {
                    menuItem5.Text = "Logout";
                    menuItem2.Enabled = true;
                    lvMain.Enabled = true;
                    HadLogin = true;
                    ka.AntiTheiveTriggered += new AntiTheiveTriggeredEvent(ka_AntiTheiveTriggered);

                    fGarden = new frmGarden(this);
                    fRanch = new frmRanch(this);
                }
                else
                    MessageBox.Show("Login error!");
                Cursor.Current = Cursors.Default;
            }
            else
            {
                Cursor.Current = Cursors.WaitCursor;
                menuItem5.Text = "Login...";
                HadLogin = false;
                menuItem2.Enabled = false;
                lvMain.Enabled = false;

                fGarden = null;
                fRanch = null;

                ka.Logout();
                Cursor.Current = Cursors.Default;
            }
        }

        private void ka_AntiTheiveTriggered(object sender, EventArgs e)
        {
            if (sender != null)
            {
                AntiTheive anti = sender as AntiTheive;
                frmAntiTheive frm = new frmAntiTheive(anti);
                frm.DoAntiTheive();
            }
        }

        private void menuItem13_Click(object sender, EventArgs e)
        {
            TextOutput.Clear();
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            fGarden.Show();
        }

        private void menuItem7_Click(object sender, EventArgs e)
        {
            fRanch.Show();
        }

        private void lvMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvMain.SelectedIndices.Count > 0)
            {
                int i = lvMain.SelectedIndices[0];
                switch (i)
                { 
                    case 0:
                        fGarden.Show();
                        break;
                    case 1:
                        fRanch.Show();
                        break;
                }
            }
        }
    }
}